package mo.offer_special;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

@SuppressWarnings("ALL")
public class L060 {

    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> num2count = new HashMap<>();
        for (int num : nums) {
            Integer count = num2count.get(num);
            if (count == null) {
                count = 1;
            } else {
                count++;
            }
            num2count.put(num, count);
        }
        PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>(((e1, e2) -> e2.getValue()-e1.getValue()));
        for (Map.Entry<Integer, Integer> entry : num2count.entrySet()) {
            queue.add(entry);
        }
        int[] res = new int[k];
        int idx = 0;
        while (idx < k) {
            res[idx] = queue.poll().getKey();
            idx++;
        }
        return res;
    }

}
